package com.distributed.common.message;

import net.sf.json.JSONObject;

import com.distributed.common.Constant;

public class VerifyRespMessage extends BaseMessage {
	public final static String ERROR = new VerifyRespMessage().setMsg("error:invaild message").toString();
	
	public VerifyRespMessage() {
		super();
		setId(Constant.VERIFY_RESP_MESSAGE);
	}

	public VerifyRespMessage(int code) {
		super();
		setId(Constant.VERIFY_RESP_MESSAGE);
		setCode(code);
	}
	
	public static String getName() {
		return Constant.VERIFY_RESP_MESSAGE_NAME;
	}

	public int getCode() {
		String code_s = argv.get(Constant.CODE);
		int code = -1;
		try {
			code = Integer.valueOf(code_s);
		} catch (NumberFormatException e) {
			code = -1;
			e.printStackTrace();
		}
		return code;
	}
	
	public void setCode(int code) {
		argv.put(Constant.CODE, String.valueOf(code));
	}
	
	public String getMsg() {
		return argv.get(Constant.MSG);
	}
	
	public VerifyRespMessage setMsg(String msg) {
		argv.put(Constant.MSG, msg);
		return this;
	}
	
	public static VerifyRespMessage getFromString(String message) {
		JSONObject jsonobject = JSONObject.fromObject(message);
		if (jsonobject == null) {
			return null;
		}
		return getFromJSON(jsonobject);
	}

	public static VerifyRespMessage getFromJSON(JSONObject jsonobj) {
		BaseMessage baseMessage = new VerifyRespMessage();
		BaseMessage baseMsg = BaseMessage.getFromJSON(jsonobj, baseMessage);
		if (baseMsg == null || baseMsg.getId() != Constant.VERIFY_RESP_MESSAGE) {
			return null;
		}
		
		return (VerifyRespMessage) baseMsg;
	}

	public JSONObject toJSON() {
		return super.toJSON();
	}
	
	public String toString() {
		return toJSON().toString();
	}
}
